import pygame
import spyral
import math

import Dic


class Bird(spyral.sprite.Sprite):
    def __init__(self, bg1, bg2, fg1, fg2, cannon):
        spyral.sprite.Sprite.__init__(self)
        self.img_1 = spyral.util.load_image(Dic.images['pig1']);
        self.img_2 = spyral.util.load_image(Dic.images['pig2']);
        self.original = self.img_1;
        self.image = self.img_1;
        self.mask = pygame.mask.from_surface(self.image);
        self.img_w = self.mask.get_size()[0];
        self.img_h = self.mask.get_size()[1];
        self.bottom = Dic.geom['height'] - self.img_h - 72;
        self.top = 640 - 65 - (self.img_h + 72);
        self.maxtop = 1920 - 65 - 72 - self.img_h
        self.layer = 1;
        self.pos = (50, self.bottom);
        
        self.xvel = 0;
        self.maxspeed = 500;
        self.yvel = 0;
        self.grav = 0;
        self.height = 0;
        self.angle = 0;
        
        self.bg1 = bg1;
        self.bg2 = bg2;
        self.fg1 = fg1;
        self.fg2 = fg2;
        self.cannon = cannon;
        
        self.done = False;
        self.launched = False;
        
    def update(self, tick):
        if (self.launched == True):
            if (20 < self.xvel):
                self.rotate(self.angle);
                self.angle -= 2;
            
            #position on screen
            x = self.cannon.cannontipx + (float(self.xvel) / self.maxspeed) * (Dic.geom['width'] - 2*self.img_w)
            if (0 <= self.yvel): #up
                if (self.height < self.top): #pass low top
                    if (self.height + self.yvel > self.top):
                        y = 65
                        self.bg1.scrollY(self.yvel - (self.top - self.height));
                        self.bg2.scrollY(self.yvel - (self.top - self.height));
                        self.fg1.scrollY(self.yvel - (self.top - self.height));
                        self.fg2.scrollY(self.yvel - (self.top - self.height));
                        self.cannon.scrollY(self.yvel - (self.top - self.height));
                    else: #lowest
                        y = self.pos[1] - (self.yvel)
                elif (self.height > self.maxtop): #highest
                    y = self.pos[1] - (self.yvel)
                    self.bg1.pos = (self.bg1.pos[0], 0);
                    self.bg2.pos = (self.bg2.pos[0], 0);
                elif (self.height < self.maxtop and self.maxtop < self.height + self.yvel): #pass highest
                    y = self.pos[1] - (self.yvel - (self.maxtop - self.height))
                    self.bg1.pos = (self.bg1.pos[0], 0);
                    self.bg2.pos = (self.bg2.pos[0], 0);
                else: #rising
                    y = 65
                    self.bg1.scrollY(self.yvel);
                    self.bg2.scrollY(self.yvel);
                    self.fg1.scrollY(self.yvel);
                    self.fg2.scrollY(self.yvel);
                    self.cannon.scrollY(self.yvel);
            elif (self.yvel < 0): #down
                if (self.pos[1] < self.bottom):
                    if (self.bottom < self.pos[1] - self.yvel): #crossing lowest
                        y = self.bottom;
                        self.bg1.scrollY(self.yvel + (self.bottom - self.pos[1]));
                        self.bg2.scrollY(self.yvel + (self.bottom - self.pos[1]));
                        self.fg1.scrollY(self.yvel + (self.bottom - self.pos[1]));
                        self.fg2.scrollY(self.yvel + (self.bottom - self.pos[1]));
                        self.cannon.scrollY(self.yvel + (self.bottom - self.pos[1]));
                    else: #lowest
                        y = self.pos[1] - self.yvel
                elif (self.height > self.maxtop): #highest
                    y = self.pos[1] - self.yvel
                else: #falling
                    y = self.bottom
                    self.bg1.scrollY(self.yvel);
                    self.bg2.scrollY(self.yvel);
                    self.fg1.scrollY(self.yvel);
                    self.fg2.scrollY(self.yvel);
                    self.cannon.scrollY(self.yvel);
            self.pos = (x, y);

            #height
            self.height += self.yvel;
            if (self.height < 0):
                self.height = 0;
            
            #bounce
            if ((self.bottom <= self.pos[1]) and (self.height == 0)):
                #reverse y
                self.yvel *= -1
                self.yvel -= self.grav;
                #slow y
                if (40 < self.yvel): 
                    self.yvel -= self.yvel / 6;
                    self.angle -= 5 
                    self.changeImage();
                else :
                    if (self.yvel == 0):
                        self.done = True;
                    else:
                        self.yvel -= 7;
                        if (self.yvel <= 0):
                            self.yvel = 0;
                    #slow x    
                if (0 < self.xvel): 
                    self.xvel -= float(self.xvel) / 15;
                #set pos to 0    
                self.pos = (self.pos[0], self.bottom)
                #set ground to correct position
                self.fg1.pos = (self.fg1.pos[0], self.fg1.bottom);
                self.fg2.pos = (self.fg2.pos[0], self.fg2.bottom);
                self.bg1.pos = (self.bg1.pos[0], self.bg1.bottom);
                self.bg2.pos = (self.bg2.pos[0], self.bg2.bottom);
            
            #gravity
            if (self.done == False):
                self.yvel += self.grav;
    
    def launch(self, speed, x, y):
        self.launched = True;
        self.height = self.bottom - self.pos[1];
        self.angle = self.cannon.angle - 90;
        self.xvel = math.cos(math.radians(self.cannon.angle)) * speed;
        self.yvel = math.sin(math.radians(self.cannon.angle)) * speed;
        self.grav = -3;
        self.movebg = True;
        self.done = False;

        
    def rotate(self, angle):
        oldcenter = self.rect.center;
        self.image = pygame.transform.rotate(self.original, angle);
        self.rect = self.image.get_rect();
        self.rect.center = oldcenter;
    
    def changeImage(self):
        if (self.original == self.img_1):
            self.original = self.img_2;
        else:
            self.original = self.img_1;
        
        
        
        